/*
*    Reversi Contender homepage:  http://code.google.com/p/reversi-contender
*
*    copyright 2011 steven sims <steven.joseph.sims at gmail.com>
*
*    This file is part of Reversi Contender.
*
*    Reversi Contender is free software: you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    Reversi Contender is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with Reversi Contender.  If not, see <http://www.gnu.org/licenses/>.
*/

package org.threadswarm.reversi.gui.animation;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.swing.SwingUtilities;
import org.jdesktop.core.animation.timing.Animator;
import org.jdesktop.core.animation.timing.TimingTarget;
import org.threadswarm.reversi.gui.CellComponent;

/**
 *
 * @author steve
 */
public class AlphaSettingTimingTarget implements TimingTarget {
    
    private final List<CellComponent> componentList;
    
    public AlphaSettingTimingTarget(Collection<CellComponent> componentCollection) {
        componentList = new ArrayList<CellComponent>(componentCollection);
    }

    @Override
    public void begin(Animator source) {}

    @Override
    public void end(Animator source) {}

    @Override
    public void repeat(Animator source) {}

    @Override
    public void reverse(Animator source) {}

    @Override
    public void timingEvent(Animator source, double fraction) {
        final float alpha = (float) fraction;
        Runnable edtRunnable = new Runnable() {
            @Override
            public void run() {
                for(CellComponent component : componentList){
                    component.setAlpha(alpha);
                    component.refresh();
                }
            }
        };
        
        if(SwingUtilities.isEventDispatchThread()){
            edtRunnable.run();
        }else{
            SwingUtilities.invokeLater(edtRunnable);
        }

    }
    
}
